/**
 *  \file kwptp.h
 *
 *  \brief
 *
 *  \author   <@linde-mh.de>
 *
 *
 */

#ifndef KWPTP_H_INCLUDED
#define KWPTP_H_INCLUDED

#ifndef KWPTP_LINKAGE
#define KWPTP_LINKAGE extern
#endif

/*================================================[ public includes  ]================================================*/
#include <windows.h>
#include "../cfg/prj.h"
#include "../cfg/kwptp_cfg.h"
/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/
struct kwptp_msg;
struct kwptp;

typedef RC (*kwptp_state_function_t)(struct kwptp *me, struct kwptp_can_msg *can_msg);
typedef RC (*kwptp_transmit_function_t)(void *obj, struct kwptp_can_msg *can_msg);
typedef RC (*kwptp_callback_function_t)(void *obj, struct kwptp_msg *msg);

enum kwptp_msg_state {

   kwptp_msg_state_sending = 0x00,
   kwptp_msg_state_pending = 0x01,
   kwptp_msg_state_timed_out = 0x02,
   kwptp_msg_state_positive_response = 0x03,
   kwptp_msg_state_negative_response = 0x04
};

struct kwptp_msg {
   enum kwptp_msg_state state;
   kwptp_callback_function_t callback;
   void* callback_data;
   U32 timeout;
   U16 len;
   U8 data[4096];
};

struct kwptp {
   kwptp_state_function_t state;
   kwptp_transmit_function_t transmit;
   void *transmit_obj;
   struct kwptp_msg *current_msg;
   DWORD timer_thread_id;
   HANDLE timer_thread_handle;
   BO timer_thread_run;
   U32 timer;
   BO use_manual_st_min;
   U32 manual_st_min;
   U32 nextIndex;
};

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/
RC kwptp_initialize(struct kwptp *me, kwptp_transmit_function_t transmit, void *transmit_obj);
RC kwptp_callback(struct kwptp *me);
RC kwptp_process(struct kwptp *me, struct kwptp_can_msg *msg);
RC kwptp_transmit(struct kwptp *me, struct kwptp_msg *msg);

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
 *                             Copyright 2010 Linde Material Handling. All rights reserved.                             *
 ***********************************************************************************************************************/
